package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.dto.WorkstationUpdateRequest;
import com.mesSystem.entity.*;
import com.mesSystem.service.impl.EmployeeService;
import com.mesSystem.service.impl.WorkstationAndEmployeeService;
import com.mesSystem.service.impl.WorkstationAndEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@ResponseBody
@RequestMapping("workstationmanagement")
public class WorkstationManagementController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    WorkstationAndEquipmentService workstationAndEquipmentService;
    @Autowired
    WorkstationAndEmployeeService workstationAndEmployeeService;

    /**
     * 获取从request中获取数据，并将员工id存入管理表中
     * 如果工位管理表中已经存在员工，则将其删除
     * 即同时拥有增加、更新（即删除）的作用
     *
     * @param request
     * @return
     */
    @PostMapping("updateEmployees")
    public Result addEmployees(@RequestBody WorkstationUpdateRequest request) {
        Long workstationId = request.getWorkstationId();
        List<Long> employeeIds = request.getEmployeeIds();
        log.info("{} + {}", workstationId, employeeIds);
        workstationAndEmployeeService.update(employeeIds, workstationId);
        return Result.ok("更新成功！");
    }

    @PostMapping("updateEquipment")
    public Result addEquipment(@RequestBody WorkstationUpdateRequest request) {
        Long workstationId = request.getWorkstationId();
        List<Long> equipmentIds = request.getEquipmentIds();
        workstationAndEquipmentService.update(equipmentIds, workstationId);
        return Result.ok("更新成功！");
    }

    // 将上述两个方法整合

    /**
     * 管理人员与设备（添加或删除）
     * @param request
     * @return
     */
    @PostMapping("management")
    public Result management(@RequestBody WorkstationUpdateRequest request) {
        Long workstationId = request.getWorkstationId();
        workstationAndEmployeeService.update(request.getEmployeeIds(), workstationId);
        workstationAndEquipmentService.update(request.getEquipmentIds(), workstationId);
        return Result.ok("更新成功");
    }

    public Result queryWorkstationName(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "size", defaultValue = "10") Integer size,
                                       @RequestParam(value = "workstationId") Long workstationId) {
        return Result.ok();
    }

    /**
     * 根据工位id分页查询员工id和名称
     *
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    @GetMapping("queryEmployeeByWorkstationId")
    public Result queryEmployeeByWorkstationId(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "size", defaultValue = "10") Integer size,
                                               @RequestParam(value = "workstationId") Long workstationId) {
        List<Employee> employees = workstationAndEmployeeService.queryEmployeeByWorkstationId(page, size, workstationId);
        return Result.ok(employees);
    }

    /**
     * 根据工位ID分页查询可供选择的员工信息（在本工位 或 不在其他岗位）
     *
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    @GetMapping("queryAvailableEmployee")
    public Result queryAvailableEmployee(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "size", defaultValue = "10") Integer size,
                                         @RequestParam(value = "workstationId") Long workstationId) {
        List<Employee> employees = workstationAndEmployeeService.queryAvailableEmployee(page, size, workstationId);
        return Result.ok(employees);
    }


    /**
     * 根据工位id分页查询设备信息
     *
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    @GetMapping("queryEquipmentByWorkstationId")
    public Result queryEquipmentByWorkstationId(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                @RequestParam(value = "workstationId") Long workstationId) {
        List<Equipment> equipments = workstationAndEquipmentService.queryEquipmentByWorkstationId(page, size, workstationId);
        return Result.ok(equipments);
    }

    /**
     * 根据工位ID分页查询可供选择的设备信息（在本工位 或 不在其他岗位）
     * @param page
     * @param size
     * @param workstationId
     * @return
     */
    @GetMapping("queryAvailableEquipment")
    public Result queryAvailableEquipment(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                          @RequestParam(value = "size", defaultValue = "10") Integer size,
                                          @RequestParam(value = "workstationId") Long workstationId) {
        List<Equipment> equipments = workstationAndEquipmentService.queryAvailableEquipment(page, size, workstationId);
        return Result.ok(equipments);
    }

}
